package com.weixin.project.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Common.Enum.ClassField;
import com.weixin.project.Dao.ExamReportDao;
import com.weixin.project.Dao.QuestionDao;
import com.weixin.project.Entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ExamService {
    @Autowired
    QuestionDao questionDao;
    @Autowired
    ExamReportDao reportDao;
    @Autowired
    PracticeService practiceService;
    private static final Integer scorePerChoice = 2;
    public List<Question> generateExamPaper(){
        List<Question> paper = new ArrayList<>();
        //计算机组成原理
        paper.addAll(generateQuestionsByClassField(ClassField.COMPUTER_COMPOSITION_PRINCIPLE, 11));
        //数据结构
        paper.addAll(generateQuestionsByClassField(ClassField.DATA_STRUCTURE, 11));
        //操作系统
        paper.addAll(generateQuestionsByClassField(ClassField.OPERATING_SYSTEM, 9));
        //计算机网络
        paper.addAll(generateQuestionsByClassField(ClassField.COMPUTER_NETWORK, 9));
        System.out.println("size of paper:" + paper.size() );
        return paper;
    }

    private List<Question> generateQuestionsByClassField(ClassField classField, int count) {
        List<Question> candidates = questionDao.
                getQuestionsByClassFieldAndIntScoreAndTypeId(
                        classField, 2, 1);
        Random random = new Random();
        List<Question> selectedQuestions = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int index = random.nextInt(candidates.size());
            selectedQuestions.add(candidates.get(index));
            candidates.remove(index);
        }
        return selectedQuestions;
    }

    public ExamReport handInPaper(JSONArray myAnswers, Integer time, Integer userId){
        ExamReport examReport = new ExamReport();
        Integer totalScore = 0;
        Integer correct = 0;
        JSONArray rightFlags = new JSONArray();
        JSONArray judgeResults = new JSONArray();
        if (myAnswers.size() != 40){
            System.out.println("Error: size of answers seems wrong!");
        }
        for (int i = 0; i < myAnswers.size(); i++) {
            JSONObject myAnswer = myAnswers.getJSONObject(i);
            Integer questionId = myAnswer.getInteger("id");
            JSONArray content = myAnswer.getJSONArray("content");
            PracticeHistory history = new PracticeHistory();
            history.setAnswer(content.toString());
            history.setTypeId(1);
            history.setQuestionId(questionId);
            practiceService.judgeAnswer(history, false);
            judgeResults.add(history.getIsRight());
            rightFlags.add(practiceService.getRightFlagsOfQuestion(questionId, 1));
            if (history.getIsRight()) {
                totalScore += 2;
                correct++;
            }
        }
        examReport.setUserId(userId);
        examReport.setTime(time);
        examReport.setRightFlags(rightFlags);
        examReport.setJudgeResult(judgeResults);
        examReport.setScore(totalScore);
        examReport.setQuestionCount(myAnswers.size());
        examReport.setCorrect(correct*1.0/myAnswers.size());
        reportDao.save(examReport);
        return examReport;
    }
}

